package com.example.processor;

/**
 * @Description: TODO
 * @Author runto
 * @Date 2023/12/17 17:17
 * @PackageName:com.example.processor
 * @ClassName: CustomBeanPostProcessor
 * @Version 1.0
 */
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.stereotype.Component;
import org.springframework.util.ReflectionUtils;
import org.springframework.web.servlet.DispatcherServlet;

import java.lang.reflect.Field;
import java.lang.reflect.Method;

@Component
public class BeanDefinitionProcessor implements BeanPostProcessor {
    @Override
    public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
        // 在初始化之前对bean进行处理
        return bean;
    }

    @Override
    public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
        // 在初始化之后对bean进行处理--可以获取到启动时候的所有bean对象
        //System.out.println(bean.toString());
        if(bean instanceof DispatcherServlet){
            //可以通过反射拿到某个属性的字段，或者更改某个值
//            System.out.println("enter DispatcherServlet...");
            Method method = ReflectionUtils.findMethod(DispatcherServlet.class, "doDispatch");
            Field field = ReflectionUtils.findField(DispatcherServlet.class, "REQUEST_TO_VIEW_NAME_TRANSLATOR_BEAN_NAME");
//            System.out.println(method);
//            System.out.println(field);
        }
        return bean;
    }
}